/**	\file		Piano.cpp
*	Adapted from Jim Idle who adapted from Java equivalent
*	\brief		This is the driver file that will generate the parser and lexer for the grammar file Piano.g
*	\author		Abbas Bazzi
*	\author		Farah Charab
*	\author		Rafah El-Khatib

*/

/**
	\mainpage
	\author		Abbas Bazzi
	\author		Farah Charab
	\author		Rafah El-Khatib
	\date		January 29,2011
*/
#include "PianoParser.h"
#include "PianoLexer.h"
#include "Converter.h"
#include "Graph.h"
#include "RealGraph.h"
#include "Builder.h"
 __NOT g;

typedef ProgGraph <string, string> Bgraph;

class TinyVisitor { public:
    bool operator() (Bgraph::ProgNode * node) {
        cout << "node " << node->data << " "<<node->idx<<endl;
        return true;
    }
    bool operator() (Bgraph::ProgEdge * edge) {
        cout << "edge " << edge <<  
            " weight " << edge->weight << 
            " from " << edge->from.idx <<
            " to " << edge->to.idx << endl;
        return true;
    }
};

typedef Bgraph::BFTraverse<TinyVisitor> TinyBFTraverse;
typedef Bgraph::DFTraverse<TinyVisitor> TinyDFTraverse;

int ANTLR3_CDECL
main (int argc, char *argv[])
{
 
    pANTLR3_UINT8    fName;


    //
    pANTLR3_INPUT_STREAM    input;




    pPianoLexer    lxr;




    pANTLR3_COMMON_TOKEN_STREAM    tstream;
    pPianoParser psr;


    if (argc < 2 || argv[1] == NULL)
    {
exit(0);
    }
    else
    {
fName = (pANTLR3_UINT8)argv[1];
    }


    input = antlr3AsciiFileStreamNew(fName);


    if ( input == NULL)
    {
   fprintf(stderr, "Failed to open file %s\n", (char *)fName);
exit(1);
}


    // Our input stream is now open and all set to go, so we can create a new instance of our
    // lexer and set the lexer input to our input stream:
    //  (file | memory | ?) --> inputstream -> lexer --> tokenstream --> parser ( --> treeparser )?
    //
    lxr    = PianoLexerNew(input);    // CLexerNew is generated by ANTLR


    // Need to check for errors
    //
    if ( lxr == NULL )
    {


   fprintf(stderr, "Unable to create the lexer due to malloc() failure1\n");
   exit(1);


    }


tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lxr));


    if (tstream == NULL)
    {
fprintf(stderr, "Out of memory trying to allocate token stream\n");
exit(1);
    }


    // Finally, now that we have our lexer constructed, we can create the parser
    //
    psr    = PianoParserNew(tstream); 


    if (psr == NULL)
    {
fprintf(stderr, "Out of memory trying to allocate parser\n");
exit(ANTLR3_ERR_NOMEM);
    }


    psr->prog(psr);


    // We did not return anything from this parser rule, so we can finish. It only remains
    // to close down our open objects, in the reverse order we created them
    //
    psr    ->free  (psr);    psr = NULL;
    tstream ->free  (tstream);    tstream = NULL;
    lxr    ->free  (lxr);    lxr = NULL;
    input   ->close (input);    input = NULL;

   //To convert From Zgraph to PianoGraph 
   //GraphStructureOne Output;
   //Output.ConvertGraphStructure(Zgraph);
   //  Output.Agraph.traverse();
 
    if(argc ==3){
	string s=argv[2];
	cout<<"PLease Make Sure Your Test File is in the bin directory"<<endl;
	g.Construct();
	RealGraph A(g.Nodes, g.Edges, g.NodeToIndex);	
	if (s== "Traverse"){ 
		A.traverse();
		cout<<"PLease Write on the commnad line   \" make aspect \" to see the input graph " <<endl;
	}
	else if( s=="Translate"){
		GraphStructureTwo ConvertedGraph;
		cout<<"PLease Write on the commnad line   \" make aspect \" to see the input graph  " <<endl;
		ConvertedGraph.ConvertGraphStructure(A);
		TinyVisitor tv;
    		TinyBFTraverse bft(tv, 0);
   		bft(ConvertedGraph.Bgraph);
	}
	else{
		cout<<"Missing Command"<<endl;
		exit(0);
	}

    }
    else if (argc==4){
	string s=argv[3];
	cout<<"PLease Make Sure Your Test File is in the bin directory"<<endl;
	g.Construct();
	RealGraph A(g.Nodes, g.Edges, g.NodeToIndex);	
	A.traverse();
	cout<<"PLease Write on the commnad line   \" make aspect \" " <<endl;
	if (s=="Check"){ 
		s=argv[2];
		CheckTree C;
		C.TreeConstructor(A,s);
		
	}
	else{
		cout<<"Missing Command"<<endl;
		exit(0);
	}
     }
   

    return 0;
}
